package com.example.android.navigation.ui

import android.os.Bundle
import android.view.*
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI
import com.example.android.navigation.R
import com.example.android.navigation.databinding.FragmentTitleBinding

class TitleFragment : Fragment() {

    private lateinit var titleBinding: FragmentTitleBinding

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {

        titleBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_title, container, false)

        titleBinding.playButton.setOnClickListener { view: View ->

            view.findNavController().navigate(TitleFragmentDirections.actionTitleFragmentToGameFragment())

        }

        /**
         * 实现用户点击“关于”菜单项时的行为
         */
        setHasOptionsMenu(true)

        return titleBinding.root;
    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.options_menu, menu)
    }

    /**
     * 覆盖在onOptionsItemSelected()单击菜单项时采取适当操作的方法。在这种情况下，操作是导航到id与所选菜单项相同的Fragment 。
     */
    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        return NavigationUI.onNavDestinationSelected(item, requireView().findNavController())
                || super.onOptionsItemSelected(item)
    }


}